home *** CD-ROM | disk | FTP | other *** search
- /**************************************************
- *
- * Sample Movie Player
- *
- * This program demonstrates how to open a movie file
- * and play it in a window
- *
- * Sample.h has definitions and equates
- * Main.c contains the general Mac application stuff
- * Movie Stuff.c has most of the interesting movie code
- *
- * Interesting things to look at:
- * How to open and close movies and players
- * Calling MoviesTask from the event loop
- * Playing movies at different speeds
- * Checking for the end of the movie while playing
- * Handling the update event for the movie window
- * Using a scroll bar to control the position of the movie
- * My cat in the about box
- *
- * Rich Williams 11/90, 2/91
- * Updated to 4.04 interfaces 4/91
- * Updated to MPW/Think 5.0/DSG interfaces 9/91 Chris Thorman
- *
- ***************************************************/
-
- /**************************************************
- *
- * General Info
- *
- ***************************************************/
- #include "Movies.h"
- #include "Mini Player.h"
-
- #include <Memory.h>
- #include <Palettes.h>
- #include <Fonts.h>
- #include <OSEvents.h>
- #include <ToolUtils.h>
- #include <Desk.h>
- #include <Resources.h>
- #include <Sound.h>
-
- /* MacHeaders Included */
-
- /* The menus */
- MenuHandle appleMenu, fileMenu, editMenu;
-
- /* The Cursors */
- Cursor waitCursor, pauseCursor, reverseCursor, playCursor, blinkCursor;
-
- /* Window stuff*/
- WindowPtr moovWindow;
- Rect dragRect;
- Rect picBounds ;
- Rect picPos = {100,100, 200, 200}; /* A starting position for the window */
-
-
-
- extern Movie m; /* The movie. We're only using one */
- extern Rect dispBounds; /* The bounds of the movie */
- extern Boolean trackingHand; /* Flag used when mouse pressed in the movie */
-
-
-
- /**************************************************
- ***************************************************
- *
- * Initialization Routines
- *
- * All of the stuff we call to get things started
- *
- ***************************************************
- ***************************************************/
-
- /**************************************************
- *
- * InitMacintosh() Initialize all the managers & memory
- *
- ***************************************************/
- void InitMacintosh()
-
- {
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitPalettes();
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- }
-
- /**************************************************
- *
- * SetUpMenus()
- *
- * Reads in the menu resources and installs them
- *
- *
- ***************************************************/
- void SetUpMenus()
-
- {
- InsertMenu(appleMenu = GetMenu(appleID), 0);
- InsertMenu(fileMenu = GetMenu(fileID), 0);
- InsertMenu(editMenu = GetMenu(editID), 0);
- DrawMenuBar();
- AddResMenu(appleMenu, 'DRVR');
-
- }
-
- /**************************************************
- *
- * SetUpWindows()
- *
- * Create the movie window.
- *
- ***************************************************/
- void SetUpWindows()
-
- {
- /* Create the window for the picture */
- dragRect = qd.screenBits.bounds;
- moovWindow = NewCWindow(0L,&picPos,(ConstStr255Param)"\p",0,4,(WindowPtr)-1L,1,0L); /* Make a new window */
- SetPort(moovWindow);
-
- }
-
- /**************************************************
- *
- * SetUpCursors() Gets the cursors we need
- *
- **************************************************/
- void SetUpCursors()
- {
- CursHandle hCurs;
-
- hCurs = GetCursor(watchCursor);
- waitCursor = **hCurs;
-
- hCurs = GetCursor(pauseCursorID);
- pauseCursor = **hCurs;
-
- hCurs = GetCursor(reverseCursorID);
- reverseCursor = **hCurs;
-
- hCurs = GetCursor(playCursorID);
- playCursor = **hCurs;
-
- hCurs = GetCursor(blinkCursorID);
- blinkCursor = **hCurs;
- }
-
- /**************************************************
- ***************************************************
- *
- * Routines for handling menus.
- *
- ***************************************************
- ***************************************************/
-
- /**************************************************
- *
- * AdjustMenus()
- *
- * Enable or disable the items in the Edit menu if a DA window
- * comes up or goes away. Our application doesn't do anything with
- * the Edit menu.
- *
- ***************************************************/
- void AdjustMenus()
- {
- register WindowPeek wp = (WindowPeek) FrontWindow();
- short kind = wp ? wp->windowKind : 0;
- Boolean DA = kind < 0, mainVis = ((WindowPeek) moovWindow)->visible;
-
- enable(editMenu, 1, DA);
- enable(editMenu, 3, DA);
- enable(editMenu, 4, DA);
- enable(editMenu, 5, DA);
- enable(editMenu, 6, DA);
-
- enable(fileMenu, openItem, !((WindowPeek) moovWindow)->visible);
- enable(fileMenu, closeItem, DA || ((WindowPeek) moovWindow)->visible);
-
- }
-
- static enable(MenuHandle menu, short item, Boolean ok)
- {
- if (ok)
- EnableItem(menu, item);
- else
- DisableItem(menu, item);
- }
-
- /**************************************************
- *
- * HandleMenu (mSelect)
- *
- * Handle the menu selection. mSelect is what MenuSelect() and
- * MenuKey() return: the high word is the menu ID, the low word
- * is the menu item
- *
- ***************************************************/
- void HandleMenu (mSelect)
-
- long mSelect;
-
- {
- int menuID = HiWord(mSelect);
- int menuItem = LoWord(mSelect);
- Str255 name;
- GrafPtr savePort;
- WindowPeek frontWindow;
-
- switch (menuID)
- {
- case appleID: /* The Apple menu */
- switch (menuItem)
- {
- case aboutItem:
- DoAboutBox(); /* Display the About Box */
- break;
-
- default: /* It's a DA */
- GetPort(&savePort);
- GetItem(appleMenu, menuItem, name);
- OpenDeskAcc(name);
- SetPort(savePort);
- break;
- }
- break;
-
- case fileID: /* The file menu */
- switch (menuItem)
- {
- case openItem:
- DoOpen(); /* Open a movie */
- break;
-
- case closeItem:
- if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
- break;
-
- if (frontWindow->windowKind < 0)
- CloseDeskAcc(frontWindow->windowKind);
- else
- /* It's the movie window ( the only one we've got ) */
- /* Close it & clean up the movie stuff */
- CloseEm( (WindowPtr) frontWindow);
-
- break;
-
-
- case quitItem: /* Pack up and go home */
- /* Clean up if movie still there */
- if (((WindowPeek) moovWindow)->visible)
- CloseEm(moovWindow);
- ExitToShell();
- break;
- }
- break;
-
- case editID:
- if (!SystemEdit(menuItem-1))
- SysBeep(5);
- break;
- }
- }
-
- /**************************************************
- *
- * CloseEm(w:WindowPtr) Closes window w
- * if w is a movie window, clean up the movie and the player
- *
- ***************************************************/
- void CloseEm(w)
-
- WindowPtr w;
-
- {
- HideWindow(w);
- if (w == moovWindow) /* If its the movie window, throw out the movie */
- {
- CleanUpMovie();
- }
- }
-
- /**************************************************
- *
- * DoAboutBox() Draws and displays the about box
- *
- ***************************************************/
- void DoAboutBox()
-
- {
- DialogPtr theDialog; /* Stuff for the about box */
- short itemHit, io;
- Handle tempHandle, soundHandle;
- Rect box;
-
- soundHandle = GetResource('snd ',meowID);
- theDialog = GetNewDialog(aboutDlgID,0L,(WindowPtr)-1L);
-
- /* Highlight the default button */
- SetPort(theDialog);
- GetDItem(theDialog,1,&itemHit,&tempHandle,&box);
- PenSize(3,3);
- InsetRect(&box,-4,-4);
- FrameRoundRect(&box,16,16);
-
- /* Wait until the button is pressed */
- do
- ModalDialog(0l,&itemHit);
- while (itemHit != 1);
-
- /* Button pressed, now meow and go home */
- io = SndPlay(0L,soundHandle,0);
- ReleaseResource(soundHandle);
- DisposDialog(theDialog);
- }
-
- /**************************************************
- *
- * DoOpen()
- *
- * Brings up standard file dialog
- * and opens the movie if one has been selected
- *
- ***************************************************/
- void DoOpen()
-
- {
- Point sFGwhere = { 90 ,82 }; /* Location of dialog for getfile */
- SFReply reply; /* Reply record from getfile */
- SFTypeList myTypeList = {'MooV'};
-
- SFGetFile(sFGwhere,(ConstStr255Param)"\p",0L,2,myTypeList,0L,&reply);
- if (reply.good)
- OpenTheMovie(reply.fName,reply.vRefNum);
-
- }
-
- /**************************************************
- ***************************************************
- *
- * The window routines
- *
- ****************************************************
- ***************************************************/
-
- /**************************************************
- *
- * AdjustCursor()
- *
- * Adjusts the cursor if it is in the movie window
- *
- ***************************************************/
- void AdjustCursor()
- {
- Point pt;
- WindowPeek wPtr;
- GrafPtr savePort;
- Boolean itsIn;
-
- if( IsMyWindow((WindowPtr) (wPtr = (WindowPeek) FrontWindow())))
- {
- GetPort(&savePort);
- SetPort((GrafPtr) wPtr);
- GetMouse(&pt);
- if (PtInRect(pt,&dispBounds)) /* In the movie window? */
- MovieCursor();
- else
- SetCursor(&qd.arrow);
-
- SetPort(savePort);
- }
- }
-
- /**************************************************
- *
- * IsMyWindow(w):Boolean
- * Checks if w is a valid window and mine
- *
- ***************************************************/
- Boolean IsMyWindow(w)
-
- WindowPtr w;
-
- {
- return( (w != 0L) &&
- (w == moovWindow) );
- }
-
- /**************************************************
- ***************************************************
- *
- * Event handling stuff
- *
- ****************************************************
- ***************************************************/
-
- /**************************************************
- *
- * HandleMouseDown (theEvent)
- *
- * Take care of mouseDown events.
- *
- ***************************************************/
- void HandleMouseDown(theEvent)
-
- EventRecord *theEvent;
-
- {
- WindowPtr theWindow;
- int windowCode = FindWindow (theEvent->where, &theWindow);
-
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (theEvent, theWindow);
- break;
-
- case inMenuBar:
- AdjustMenus();
- HandleMenu(MenuSelect(theEvent->where));
- break;
-
- case inDrag:
- if (IsMyWindow(theWindow))
- DragWindow(theWindow, theEvent->where, &dragRect);
- break;
-
- case inContent:
- if (IsMyWindow(theWindow))
- {
- if (theWindow != FrontWindow())
- SelectWindow(theWindow);
- else
- MovieMouseDown(theWindow,theEvent->where); /* Track the mouse */
- }
- break;
-
- case inGoAway:
- if (IsMyWindow(theWindow) && TrackGoAway(theWindow, theEvent->where))
- CloseEm(theWindow); /* Close the appropriate windows */
- break;
-
- break;
- }
- }
-
- /**************************************************
- *
- * HandleMouseUp (theEvent)
- *
- * Take care of mouseUp events for tracking the hand.
- *
- ***************************************************/
- void HandleMouseUp(theEvent)
-
- EventRecord *theEvent;
-
- {
- WindowPtr theWindow;
- int windowCode = FindWindow (theEvent->where, &theWindow);
-
- if( windowCode == inContent )
- MovieMouseUp(theWindow,theEvent->where);
-
- trackingHand = false;
- }
-
- /**************************************************
- *
- * HandleEvent()
- *
- * The main event dispatcher. This routine should be called
- * repeatedly (it handles only one event).
- *
- ***************************************************/
- void HandleEvent()
-
- {
- int ok;
- EventRecord theEvent;
- WindowPtr theWindow;
- OSErr retstat;
-
- HiliteMenu(0);
- SystemTask (); /* Handle desk accessories */
- AdjustCursor(); /* Adjust the cursor as needed */
-
- /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !
- ! Don't miss this!
- ! MoviesTask is called from the main event loop to
- ! give time to the player
- !
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
- MyMoviesTask();
-
- ok = GetNextEvent (everyEvent, &theEvent);
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- HandleMouseDown(&theEvent);
- break;
-
- case mouseUp:
- HandleMouseUp(&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- AdjustMenus();
- HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
- }
- break;
-
- case updateEvt:
- /* To update the window, make the player replay the last frame */
- theWindow = (WindowPtr) theEvent.message; /* Get the window */
-
- BeginUpdate(theWindow);
- if(theWindow == moovWindow)
- DoMovieUpdate();
- DrawControls(theWindow);
- EndUpdate(theWindow);
- break;
-
- case activateEvt:
- /* Force an update event */
- SetPort( (WindowPtr) theEvent.message );
- InvalRect(&((WindowPtr)theEvent.message)->portRect);
- break;
- }
- }
-
- /**************************************************
- *
- * main()
- *
- * This is where everything happens
- *
- ***************************************************/
- void main()
-
- {
- InitMacintosh(); /* Initialize everything */
- SetUpMenus();
- SetUpCursors();
- SetUpWindows();
- SetUpMovies();
-
- for (;;)
- HandleEvent();
- }
-